package com.lxj.database.user;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.lxj.database.SQLiteDB;
import com.lxj.model.Pic;
import com.lxj.model.UserInfo;
import com.lxj.utils.L;


public class UserInfoStorage 
{
	private static UserInfoStorage mInstance = null;

	private SQLiteDB mSqliteDB = null;

	public UserInfoStorage(Context aContext) {
		mSqliteDB = SQLiteDB.getInstance(aContext);
	}

	public static UserInfoStorage getInstance(Context aContext) {
		if (null == mInstance) {
			mInstance = new UserInfoStorage(aContext);
		}
		return mInstance;
	}
	
	public boolean delUserInfoInfo(int aUserID)
	{
		String where = UserInfoTable.USER_ID + "='" + aUserID + "'";
		int count = mSqliteDB.delete(UserInfoTable.getTableName(), where, null);
		
		if(count >= 0)
		{
			UserInfoObserver.getInstance().OnUserRemove(aUserID);
		}
		return count > 0;
	}
	
	public void setUserInfoInfo(UserInfo aUserInfo)
	{	
		if(aUserInfo != null)
		{
			ContentValues localContentValues = new ContentValues();
			
			localContentValues.put(UserInfoTable.PIC_ID, aUserInfo.getUser_head().getPic_id());
			localContentValues.put(UserInfoTable.PIC_URL, aUserInfo.getUser_head().getPic_url());
			localContentValues.put(UserInfoTable.NAME, aUserInfo.getUser_name());
			localContentValues.put(UserInfoTable.BIRTHDAY, aUserInfo.getUser_birthday());
			localContentValues.put(UserInfoTable.INTRODUCTION, aUserInfo.getUser_introduction());
			localContentValues.put(UserInfoTable.SEX, aUserInfo.getUser_sex());
			localContentValues.put(UserInfoTable.USER_ID, aUserInfo.getUser_id());
			
			String where = UserInfoTable.USER_ID + "='" + aUserInfo.getUser_id() + "'";
			
			int rowscount = mSqliteDB.update(UserInfoTable.getTableName(), localContentValues, where, null);
			L.e("rowscount"+rowscount);
			if(rowscount <= 0)
			{
				localContentValues.put(UserInfoTable.USER_ID, aUserInfo.getUser_id());
				mSqliteDB.insert(UserInfoTable.getTableName(), null, localContentValues);
				if(!mSqliteDB.inTransaction())
				{
					UserInfoObserver.getInstance().OnUserAdd(aUserInfo);
				}
			}
		}
	}
	
	
	
	public void setUserInfoInfos(UserInfo[] aUserinfo)
	{	
		if(aUserinfo != null && aUserinfo.length > 0)
		{
			mSqliteDB.beginTransaction();
			for(UserInfo t : aUserinfo)
			{
				setUserInfoInfo(t);
			}
			mSqliteDB.commitTransaction();
			mSqliteDB.endTransaction();
		}
	}
	public UserInfo getUserInfoList(String aUserID)
	{
		UserInfo ret = null;
		String sql = "select * from " + UserInfoTable.getTableName() + " where " + UserInfoTable.USER_ID + " = '" + aUserID + "'";
		
		Cursor c = mSqliteDB.execSql(sql, null);
		
		if(c.moveToFirst())
		{
			ret = new UserInfo();
			int index_userid = c.getColumnIndex(UserInfoTable.USER_ID);
			int index_name= c.getColumnIndex(UserInfoTable.NAME);
			int index_sex=c.getColumnIndex(UserInfoTable.SEX);
			int index_birthday=c.getColumnIndex(UserInfoTable.BIRTHDAY);
			int index_picid=c.getColumnIndex(UserInfoTable.PIC_ID);
			int index_picurl=c.getColumnIndex(UserInfoTable.PIC_URL);
			int index_introduction=c.getColumnIndex(UserInfoTable.INTRODUCTION);
			

			int userid = c.isNull(index_userid) ? 0 : c.getInt(index_userid);
			String name = c.isNull(index_name) ? null : c.getString(index_name);
			int sex = c.isNull(index_sex) ? 0 : c.getInt(index_sex);
			Long birthday = c.isNull(index_birthday) ? 0 : c.getLong(index_birthday);
			int pic_id = c.isNull(index_picid) ? 0 : c.getInt(index_picid);
			String pic_url = c.isNull(index_picurl) ? null : c.getString(index_picurl);
			String introduction = c.isNull(index_introduction) ? null : c.getString(index_introduction);
			
			Pic head = new Pic();
			head.setPic_id(pic_id);
			head.setPic_url(pic_url);
			ret.setUser_head(head);
			ret.setUser_name(name);
			ret.setUser_id(userid);
			ret.setUser_sex(sex);
			ret.setUser_birthday(birthday);
			ret.setUser_introduction(introduction);
		}
		c.close();
		
		return ret;
	}
	
	
	
	
	
	
	public UserInfo[] getUserInfoList()
	{
		UserInfo[] ret = null;
		
		String sql = "select * from " + UserInfoTable.getTableName() ;
		
		Cursor c = mSqliteDB.execSql(sql, null);
		
		if(c.moveToFirst())
		{
			int count = c.getCount();
			int offset = 0;
			
			ret = new UserInfo[count];
			
			int index_userid = c.getColumnIndex(UserInfoTable.USER_ID);
			int index_name= c.getColumnIndex(UserInfoTable.NAME);
			int index_sex=c.getColumnIndex(UserInfoTable.SEX);
			int index_birthday=c.getColumnIndex(UserInfoTable.BIRTHDAY);
			int index_picid=c.getColumnIndex(UserInfoTable.PIC_ID);
			int index_picurl=c.getColumnIndex(UserInfoTable.PIC_URL);
			int index_introduction=c.getColumnIndex(UserInfoTable.INTRODUCTION);
			
			do
			{
				int userid = c.isNull(index_userid) ? 0 : c.getInt(index_userid);
				String name = c.isNull(index_name) ? null : c.getString(index_name);
				int sex = c.isNull(index_sex) ? 0 : c.getInt(index_sex);
				Long birthday = c.isNull(index_birthday) ? 0 : c.getLong(index_birthday);
				int pic_id = c.isNull(index_picid) ? 0 : c.getInt(index_picid);
				String pic_url = c.isNull(index_picurl) ? null : c.getString(index_picurl);
				String introduction = c.isNull(index_introduction) ? null : c.getString(index_introduction);
				

				ret[offset] = new UserInfo();
				
				Pic head = new Pic();
				
				head.setPic_id(pic_id);
				head.setPic_url(pic_url);
				
				ret[offset].setUser_head(head);
				ret[offset].setUser_name(name);
				ret[offset].setUser_id(userid);
				ret[offset].setUser_sex(sex);
				ret[offset].setUser_birthday(birthday);
				ret[offset].setUser_introduction(introduction);
				
				offset++;
			}
			while(c.moveToNext());
		}
		c.close();
		
		return ret;
	}
	
}
